package comparatorTest;

import java.util.Arrays;

 

public class ComparatorTest {

 

    @SuppressWarnings("unchecked")

    public static void main(String[] args) {

       Dog o1 = new Dog("dog1", 1, 5);

       Dog o2 = new Dog("dog2", 2, 4);

       Dog o3 = new Dog("dog3", 3, 3);

       Dog o4 = new Dog("dog4", 4, 2);

       Dog o5 = new Dog("dog5", 5, 1);

 

       Dog[] dogs = new Dog[] { o1, o4, o3, o5, o2 };

 

       System.out.println("未排序前");

       for (int i = 0; i < dogs.length; i++) {

           Dog dog = dogs[i];

           System.out.println(dog.getName());

       }

 

       Arrays.sort(dogs, new ByHeightComparator());

       System.out.println("使用高度排序之后：");

       for (int i = 0; i < dogs.length; i++) {

           Dog dog = dogs[i];

           System.out.println(dog.getName());

       }

 

       Arrays.sort(dogs, new ByWeightComparator());

       System.out.println("使用重量排序之后：");

       for (int i = 0; i < dogs.length; i++) {

           Dog dog = dogs[i];

           System.out.println(dog.getName());

       }

 

    }

}

